//
//  ViewController.m
//  CodeFactory
//
//  Created by daibaisheng on 2019/4/29.
//  Copyright © 2019 daibaisheng. All rights reserved.
//

#import "ViewController.h"
#import "CreateManager.h"
#import "NSFileManager+Tools.h"


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    
}
- (IBAction)clickcreate:(id)sender {
    
    if (![[self class]isBlankString:self.textfiled.stringValue]) {

        [[CreateManager sharedInstance]CreateCoder:self.textfiled.stringValue];
        
    }else
    {
        NSAlert *alert = [[NSAlert alloc] init];
        alert.alertStyle = NSAlertStyleWarning;
        alert.messageText = @"请输入类名";
        [alert beginSheetModalForWindow:[NSApplication sharedApplication].keyWindow completionHandler:^(NSModalResponse returnCode) {
        }];
        
            return;
        
    }

    [self openFileManger:nil];
    
}
+  (BOOL)isBlankString:(NSString *)aStr {
    if (!aStr) {
        return YES;
    }
    if ([aStr isKindOfClass:[NSNull class]]) {
        return YES;
    }
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmedStr = [aStr stringByTrimmingCharactersInSet:set];
    if (!trimmedStr.length) {
        return YES;
    }
    return NO;
}

- (IBAction)remove:(id)sender {
    
    
    NSAlert *alert = [[NSAlert alloc] init];
     alert.alertStyle = NSAlertStyleWarning;
    [alert addButtonWithTitle:@"确定"];
    [alert addButtonWithTitle:@"取消"];
     alert.messageText = @"删除文件夹?";
     alert.informativeText = @"确定要删除整个文件夹么,删除后无法恢复？";
     [alert beginSheetModalForWindow:[NSApplication sharedApplication].keyWindow completionHandler:^(NSModalResponse returnCode) {
           if (returnCode == NSAlertFirstButtonReturn) {

             [[CreateManager sharedInstance] removeFiles];
            
             
                     } else if (returnCode == NSAlertSecondButtonReturn) {
                 
                             NSLog(@"取消");
                 
                         }
    }];
}


- (IBAction)openFileManger:(id)sender {
    

    NSOpenPanel *panel = [NSOpenPanel openPanel];
    
    
    NSURL * url = [NSURL URLWithString:[CreateManager returnPath]];

    if ([NSFileManager isExistAtPath:[CreateManager returnPath]]) {
        
        
        
    }else
    {
        NSAlert *alert = [[NSAlert alloc] init];
        alert.alertStyle = NSAlertStyleWarning;
        alert.messageText = @"文件夹不存在，请先生成代码";
        [alert beginSheetModalForWindow:[NSApplication sharedApplication].keyWindow completionHandler:^(NSModalResponse returnCode) {
        }];
        return;
        
        
    }
    
    [panel setAllowsMultipleSelection:YES];
    
    [panel setDirectoryURL:url];

    [panel runModal];
    
    
    
}



@end
